import java.util.*;
import java.util.Collections;
import java.util.stream.Collectors;

public class Text {
    public boolean isValidSudoku(char[][] board) {
        int[][] row = new int[9][9];
        int[][] col = new int[9][9];
        int[][][] night = new int[3][3][9];
        for(int i = 0; i < 9; i++) {
            for(int j = 0; j < 9; j++) {
                char ch = board[i][j];
                if(ch != '.') {
                    int index = ch - '1';
                    row[i][index]++;
                    col[j][index]++;
                    night[i/3][j/3][index]++;
                    if(row[i][index] > 1 || col[j][index] > 1 || night[i/3][j/3][index] > 1){
                        return false;
                    }
                }
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int[][] array = {{0,1,2,0},{3,4,5,2},{1,3,1,5}};
        setZeroes(array);
    }
    public static void setZeroes(int[][] matrix) {
        int r = matrix.length;
        int c = matrix[0].length;
        boolean flagrow = false;
        boolean flagcol = false;
        for(int i = 0; i < r; i++) {
            if(matrix[i][0] == 0) {
                flagrow = true;
            }
        }
        for(int j = 0; j < c; j++) {
            if(matrix[0][j] == 0) {
                flagcol = true;
            }
        }
        for(int i = 1; i < r; i++) {
            for(int j = 1;j < c; j++) {
                if(matrix[i][j] == 0){
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for(int i = 1; i < r; i++) {
            for(int j = 1;j < c; j++) {
                if(matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }
        if(flagrow) {
            for(int i = 0; i < r; i++) {
                matrix[i][0] = 0;
            }
        }
        if(flagcol) {
            for(int j = 0; j < c; j++) {
                matrix[0][j] = 0;
            }
        }
    }

}
